<?php

class ProductPriceController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{

		$input =Input::all();
			$input['_id']='PROPR'.date('YmdHis');
			$input['productid']='PRO20140527114647';
			$input['categoryid']='CAT20140527091515';
			$input['price']='1';
			$input['sellprice']="Electronic";
			$input['startdate']=date('Y-m-d H:i:s');
			$input['enddate']=date('Y-m-d H:i:s');
		$validation=ProductPrice::validation($input);


		if($validation->fails()){
			return $message=$validation->messages();//->toArray(); 
		}else{


			$AvailCat=DB::table('category')
 				 ->where('_id',$input['categoryid'])->first();			
			
		  		if($AvailCat==null){
					return "Category Id You have provided isnt right";
		  		}else{
		  		
		  			$AvailPro=DB::table('product')
    				->where('_id',$input['productid'])->first();			
						if($AvailPro==null){
								return "Product Id You have provided isnt right";	
						}else{
								$user=ProductPrice::create($input);
								return "Data Added SuccessFully";
						}
		  		}		  	
			
		}	
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
					
		 $product = DB::table('productprice')->where('_id', $id)->first();
        if ($product) {
          	
        print_r($product);
           // return Response::json(ResponseManager::getResult($feeStructure, $message));
        	
        } else {
           return $message = 'No Match Found';
           // return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	

	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$data = productprice::where('_id', '=', $id) ->first(); 
		
		$input=Input::all();
			$input['_id']='PROPR'.date('YmdHis');
			$input['productid']='mobile';
			$input['categoryid']='abcd';
			$input['price']='1';
			$input['sellprice']="Electronic";
			$input['startdate']=date('Y-m-d H:i:s');
			$input['enddate']=date('Y-m-d H:i:s');		
		
		//$input['_id']='CAT20140527090747';	
		//$data = Category::where('_id', '=', $id)->update($input);
		
			//$data->_id=$id;
		$validation=productprice::validation($input);
		$lenth=count($data);	
			if($lenth=="1"){	
				if($validation->fails()){
					return $message=$validation->messages();//->toArray(); 
					return $id;
				  }else{
					

				  	       // $input = Input::all();
				  	$input = Input::except('_token');

        					foreach ($input as $key => $value) {

            						$update = productprice::find($key);
            						$update->value = $value;
            						$update->save();
        							}
					return "Data updated SuccessFully";
		}
			}else
		{
			return "No Record Found";
		}
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
				$data = productprice::where('_id', '=', $id) ->first(); 
		
		$input=Input::all();
			$input['_id']=$id;
			$input['productid']='mobile111';
			$input['categoryid']='abcd111';
			$input['price']='111';
			$input['sellprice']="Electronic111";
			$input['startdate']=date('Y-m-d H:i:s');
			$input['enddate']=date('Y-m-d H:i:s');		
		
		//$input['_id']='CAT20140527090747';	
		//$data = Category::where('_id', '=', $id)->update($input);
		
			//$data->_id=$id;
		$validation=productprice::validation($input);
		$lenth=count($data);	
			if($lenth=="1"){	
				if($validation->fails()){
					return $message=$validation->messages();//->toArray(); 
					return $id;
				  }else{
					
				  	$productprice=DB::table('productprice')
				  				->where('_id',$id)
				  				->update($input);	
					return "Data updated SuccessFully";
		}
			}else
		{
			return "No Record Found";
		}	
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
	
	 $product = productprice::find($id);
        
//        var_dump($feeStructure);
        
        if($product){
          $product->delete();
        }else{
        	return "NO RECORD TO DELETE";
        }
        if ($product) {
         return   $message = 'Deleted Successfully.';
            //return Response::json(ResponseManager::getResult($feeStructureData->toArray(), $message));
        } else {
           return  $message = 'Ooopss...! Not deleted.';
            //return Response::json(ResponseManager::getError(array(), 01, $message));
        }
	}


	}
